import { downloadFile } from "@/service/global";
import { message } from "antd";

/**
 *
 * @param blobData 导出用到的blobData，直接使用request返回的数据
 * @param name 导出文件名
 * @param isChart 是否是图表那种直接走该方法，文件名不带后缀的的自定义导出excel
 */
export const exportExcel = (blobData: Blob, name: string, isChart?: boolean) => {
  // console.log('blobData:', blobData);
  console.log('blobData:', blobData,typeof blobData);

  const reader = new FileReader();
  try {
    reader.readAsDataURL(blobData);
    reader.onload = () => {
      const link: any = document.createElement('a');
      link.href = reader.result;
      if (name.indexOf('.') > -1 && isChart) {
        link.download = name + '.xls';
      } else {
        link.download = name;
      }
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    };
  } catch (error) {
    console.log(error);

    message.error(error);
  }
};

/**
 *
 * @param blobData 导出用到的blobData，直接使用request返回的数据
 * @param name 导出文件名
 */
 const viewFile = (blobData: Blob, name: string) => {
  console.log('blobData:', blobData);
  const reader = new FileReader();
  try {
    reader.readAsDataURL(blobData);
    reader.onload = () => {
      const link: any = document.createElement('a');
      link.href = reader.result;
      // console.log('reader.result', reader.result)
      const _imgUrl = reader.result;
      let PDFData = _imgUrl.replace("data:application/octet-stream;base64,", "");
      sessionStorage.setItem("_imgUrl", PDFData);
      // console.log('REACT_APP_ENV',REACT_APP_ENV)
      if (process.env.NODE_ENV == "production") {
        top.window.postMessage(
          { url: `/report-center/Pdf/web/viewer.html?id=${name}&name=${name}` },
          window.location.origin,
        );
        // window.open(`/report-center/Pdf/web/viewer.html?id=${name}&name=${name}`);
      } else {
        window.open(`/Pdf/web/viewer.html?id=${name}&name=${name}`);
      }

    };
  } catch (error) {
    console.log(error);

    message.error(error);
  }
};


// 现在后端支持的是pdf，doc，png
export const DownLoad_File = async (
  bucketName: string,
  fileName: string,
  originalName?: string,
  type?: string,
) => {
  console.log('xixxxxxxxxxxxxxxxxxx');

  const response: Blob = await downloadFile({ bucketName, fileName })
    .then((res) => {
      // console.log('res:', res)
      return res;
    })
    .catch((err) => {
      message.error('下载失败，请检查您的网络设置');
    });
  // console.log('response:', response)
  // const response: Blob = await downloadFile({ bucketName, fileName })
  // const fileType = ['pdf', 'doc', 'png']
  // 尽调记录那里 fileName 形如 ad89b64b1ab4466aabafa8524ae66ff3.doc.1609824030134
  // 需要转一下
  // console.log('download***********');
  console.log(bucketName, fileName, originalName, type, response, '-----------22----');

  const file = fileName.split('.')[0];
  if (!response) {
    message.error('下载失败，请检查您的网络设置');
    return false;
  }
  if (type == "view") {
    viewFile(response, originalName || file);
  } else {
    exportExcel(response, originalName || file);
  }

  return false;
};